package com.eqdd.yiqidian.utils;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.eqdd.yiqidian.base.AppConstent;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.User;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.http.JsonCallBack;
import com.eqdd.yiqidian.ui.LoginActivity;
import com.eqdd.yiqidian.ui.MineAuActivity;
import com.eqdd.yiqidian.ui.mine.shenqing.ApplyLizhiActivity;
import com.eqdd.yiqidian.widget.ChooseAlertView;
import com.eqdd.yiqidian.widget.EditDialogFragment;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.HashMap;
import java.util.List;

/**
 * Created by lv on 17-9-11.
 */

public class ActivityInterceptor {
    private static boolean isNetGet = false;
    private static String[] auChecks = new String[]{
            "QiyeActivity",
            "WorkCircleActivity",
    };
    private static String[] isInComChecks = new String[]{

    };
    private static String[] bothChecks = new String[]{
            "MyPizhunActivity",
            "MyShenqingActivity",
            "RenwuActivity",
            "DakaActivity",
            "MyKanbanActivity",
            "GonggaoActivity",
            "RenshiActivity",
            "StaffActivity"
    };

    private static String[] isAdminChecks = new String[]{
            "ApplyLizhiActivity"
    };


    public static void interceptor(BaseActivity activity, Class clazz, Bundle bundle, OnInterceptor onInterceptor) {

        for (String bothCheck : bothChecks) {
            if (clazz.getName().contains(bothCheck)) {
                bothCheck(activity, bundle, onInterceptor);
                return;
            }
        }
        for (String auCheck : auChecks) {
            if (clazz.getName().contains(auCheck)) {
                auCheck(activity, bundle, onInterceptor);
                return;
            }
        }
        for (String isInComCheck : isInComChecks) {
            if (clazz.getName().contains(isInComCheck)) {
                isInComCheck(activity, bundle, onInterceptor);
                return;
            }
        }
        for (String isAdminCheck : isAdminChecks) {

            if (clazz.getName().contains(isAdminCheck)) {
                isInAdminCheck(activity, bundle, onInterceptor);
                return;
            }
        }
        if (clazz.getName().contains("MineAuActivity")) {
            if (isNetGet) {
                HashMap<String, String> map = new HashMap<>();
                map.put("u1", GreenDaoUtil.getUser().getUname());
                map.put("u2", EncryptUtil.sha1(SPUtil.getParam(Config.PASS) + "EQD"));
                OkGo.<HttpResult<User>>post(HttpConfig.BASE_URL + HttpConfig.LOGIN)
                        .params(map)
                        .execute(new JsonCallBack<HttpResult<User>>() {
                            @Override
                            public void onSuccess(Response<HttpResult<User>> response) {
                                HttpResult<User> httpResult = response.body();
                                if (httpResult.getStatus() == 200) {
                                    if (httpResult.getItems().getIsAdmin() == 2) {
                                        onInterceptor.go(bundle);
                                    } else {
                                        ToastUtil.showShort("你已通过实名认证");
                                    }
                                }else {
                                    AlertDialog alertDialog = ChooseAlertView.getAlertDialog(activity, "提醒", "身份校验失败,请重新登陆!", new EditDialogFragment.OnClickListener() {
                                        @Override
                                        public void onClick(List<String> contents) {
                                            GreenDaoUtil.deleteAllUser();
                                            Intent intent1 = new Intent(activity, LoginActivity.class);
                                            activity.startActivity(intent1);
                                            activity.finish();
                                        }
                                    });
                                    alertDialog.show();
                                }
                            }
                        });
            } else {
                if (AppConstent.user.getAuthen() == 0) {
                    onInterceptor.go(bundle);
                } else {
                    ToastUtil.showShort("你已通过实名认证");
                }
            }
        } else {
            onInterceptor.go(bundle);
        }

    }

    /**
     * 检测身份不为老板,放行
     *
     * @param activity
     * @param bundle
     * @param onInterceptor
     */
    private static void isInAdminCheck(BaseActivity activity, Bundle bundle, OnInterceptor onInterceptor) {
        if (isNetGet) {
            HashMap<String, String> map = new HashMap<>();
            map.put("u1", GreenDaoUtil.getUser().getUname());
            map.put("u2", EncryptUtil.sha1(SPUtil.getParam(Config.PASS) + "EQD"));
            OkGo.<HttpResult<User>>post(HttpConfig.BASE_URL + HttpConfig.LOGIN)
                    .params(map)
                    .execute(new JsonCallBack<HttpResult<User>>() {
                        @Override
                        public void onSuccess(Response<HttpResult<User>> response) {
                            HttpResult<User> httpResult = response.body();
                            if (httpResult.getStatus() == 200) {
                                if (httpResult.getItems().getIsAdmin() == 2) {
                                    ToastUtil.showShort("该身份不符合条件");
                                } else {
                                    onInterceptor.go(bundle);
                                }
                            }else {
                                AlertDialog alertDialog = ChooseAlertView.getAlertDialog(activity, "提醒", "身份校验失败,请重新登陆!", new EditDialogFragment.OnClickListener() {
                                    @Override
                                    public void onClick(List<String> contents) {
                                        GreenDaoUtil.deleteAllUser();
                                        Intent intent1 = new Intent(activity, LoginActivity.class);
                                        activity.startActivity(intent1);
                                        activity.finish();
                                    }
                                });
                                alertDialog.show();
                            }
                        }
                    });
        } else {
            if (AppConstent.user.getIsAdmin() == 2) {
                ToastUtil.showShort("该身份不符合条件");

            } else {
                onInterceptor.go(bundle);
            }
        }
    }

    /**
     * 检测有企业,放行,
     *
     * @param activity
     * @param bundle
     * @param onInterceptor
     */
    private static void isInComCheck(BaseActivity activity, Bundle bundle, OnInterceptor onInterceptor) {
        if (isNetGet) {
            HashMap<String, String> map = new HashMap<>();
            map.put("u1", GreenDaoUtil.getUser().getUname());
            map.put("u2", EncryptUtil.sha1(SPUtil.getParam(Config.PASS) + "EQD"));
            OkGo.<HttpResult<User>>post(HttpConfig.BASE_URL + HttpConfig.LOGIN)
                    .params(map)
                    .execute(new JsonCallBack<HttpResult<User>>() {
                        @Override
                        public void onSuccess(Response<HttpResult<User>> response) {
                            HttpResult<User> httpResult = response.body();
                            if (httpResult.getStatus() == 200) {
                                if (httpResult.getItems().getCompanyId() == 0) {
                                    ToastUtil.showShort("请先加入一个企业");
                                } else {
                                    onInterceptor.go(bundle);
                                }
                            }else {
                                AlertDialog alertDialog = ChooseAlertView.getAlertDialog(activity, "提醒", "身份校验失败,请重新登陆!", new EditDialogFragment.OnClickListener() {
                                    @Override
                                    public void onClick(List<String> contents) {
                                        GreenDaoUtil.deleteAllUser();
                                        Intent intent1 = new Intent(activity, LoginActivity.class);
                                        activity.startActivity(intent1);
                                        activity.finish();
                                    }
                                });
                                alertDialog.show();
                            }
                        }
                    });
        } else {
            if (AppConstent.user.getCompanyId() == 0) {
                ToastUtil.showShort("请先加入一个企业");
            } else {
                onInterceptor.go(bundle);
            }
        }
    }

    /**
     * 检测实名与拥有企业放行
     *
     * @param activity
     * @param bundle
     * @param onInterceptor
     */
    private static void bothCheck(BaseActivity activity, Bundle bundle, OnInterceptor onInterceptor) {
        if (isNetGet) {
            HashMap<String, String> map = new HashMap<>();
            map.put("u1", GreenDaoUtil.getUser().getUname());
            map.put("u2", EncryptUtil.sha1(SPUtil.getParam(Config.PASS) + "EQD"));
            OkGo.<HttpResult<User>>post(HttpConfig.BASE_URL + HttpConfig.LOGIN)
                    .params(map)
                    .execute(new JsonCallBack<HttpResult<User>>() {
                        @Override
                        public void onSuccess(Response<HttpResult<User>> response) {
                            HttpResult<User> httpResult = response.body();
                            if (httpResult.getStatus() == 200) {
                                if (httpResult.getItems().getAuthen() == 1) {
                                    if (httpResult.getItems().getCompanyId() == 0) {
                                        ToastUtil.showShort("请先加入一个企业");
                                    } else {
                                        onInterceptor.go(bundle);
                                    }
                                } else {
                                    new MaterialDialog.Builder(activity)
                                            .title("提醒")
                                            .content("你还未实名认证？")
                                            .positiveText("前往认证")
                                            .negativeText("返回")
                                            .onAny((dialog, which) -> {
                                                if (which == DialogAction.POSITIVE) {
                                                    activity.startActivity(MineAuActivity.class, true);
                                                }
                                            }).show();
                                }
                            }else {
                                AlertDialog alertDialog = ChooseAlertView.getAlertDialog(activity, "提醒", "身份校验失败,请重新登陆!", new EditDialogFragment.OnClickListener() {
                                    @Override
                                    public void onClick(List<String> contents) {
                                        GreenDaoUtil.deleteAllUser();
                                        Intent intent1 = new Intent(activity, LoginActivity.class);
                                        activity.startActivity(intent1);
                                        activity.finish();
                                    }
                                });
                                alertDialog.show();
                            }
                        }
                    });
        } else {
            if (AppConstent.user.getAuthen() == 1) {
                if (AppConstent.user.getCompanyId() == 0) {
                    ToastUtil.showShort("请先加入一个企业");
                } else {
                    onInterceptor.go(bundle);
                }
            } else {
                new MaterialDialog.Builder(activity)
                        .title("提醒")
                        .content("你还未实名认证？")
                        .positiveText("前往认证")
                        .negativeText("返回")
                        .onAny((dialog, which) -> {
                            if (which == DialogAction.POSITIVE) {
                                activity.startActivity(MineAuActivity.class, true);
                            }
                        }).show();
            }
        }
    }

    /**
     * 检测实名放行
     *
     * @param activity
     * @param bundle
     * @param onInterceptor
     */
    private static void auCheck(BaseActivity activity, Bundle bundle, OnInterceptor onInterceptor) {

        if (isNetGet) {
            HashMap<String, String> map = new HashMap<>();
            map.put("u1", GreenDaoUtil.getUser().getUname());
            map.put("u2", EncryptUtil.sha1(SPUtil.getParam(Config.PASS) + "EQD"));
            OkGo.<HttpResult<User>>post(HttpConfig.BASE_URL + HttpConfig.LOGIN)
                    .params(map)
                    .execute(new JsonCallBack<HttpResult<User>>() {
                        @Override
                        public void onSuccess(Response<HttpResult<User>> response) {
                            HttpResult<User> httpResult = response.body();
                            if (httpResult.getStatus() == 200) {
                                if (httpResult.getItems().getAuthen() == 1) {
                                    onInterceptor.go(bundle);
                                } else {
                                    new MaterialDialog.Builder(activity)
                                            .title("提醒")
                                            .content("你还未实名认证？")
                                            .positiveText("前往认证")
                                            .negativeText("返回")
                                            .onAny((dialog, which) -> {
                                                if (which == DialogAction.POSITIVE) {
                                                    activity.startActivity(MineAuActivity.class);
                                                }
                                            }).show();
                                }
                            } else {
                                AlertDialog alertDialog = ChooseAlertView.getAlertDialog(activity, "提醒", "身份校验失败,请重新登陆!", new EditDialogFragment.OnClickListener() {
                                    @Override
                                    public void onClick(List<String> contents) {
                                        GreenDaoUtil.deleteAllUser();
                                        Intent intent1 = new Intent(activity, LoginActivity.class);
                                        activity.startActivity(intent1);
                                        activity.finish();
                                    }
                                });
                                alertDialog.show();
                            }
                        }
                    });
        } else {
            if (AppConstent.user.getAuthen() == 1) {
                onInterceptor.go(bundle);
            } else {
                new MaterialDialog.Builder(activity)
                        .title("提醒")
                        .content("你还未实名认证？")
                        .positiveText("前往认证")
                        .negativeText("返回")
                        .onAny((dialog, which) -> {
                            if (which == DialogAction.POSITIVE) {
                                activity.startActivity(MineAuActivity.class, true);
                            }
                        }).show();
            }
        }
    }

    public interface OnInterceptor {
        void go(Bundle bundle);
    }
}
